package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.InstitutionsListVo;
import com.indexingsystem.boss.Vo.NotBugOrderProductListVo;
import com.indexingsystem.boss.Vo.OrderAddProductInitVo;
import com.indexingsystem.boss.Vo.OrderCateReqVo;
import com.indexingsystem.boss.Vo.OrderProductBugVo;
import com.indexingsystem.boss.Vo.OrderProductListQueryVo;
import com.indexingsystem.boss.Vo.ResCategorsRelationQueryVo;
import com.indexingsystem.boss.Vo.ResCategoryOrderQueryVo;
import com.indexingsystem.boss.Vo.ResRelationQueryVo;
import com.indexingsystem.boss.Vo.ResourceCategorsSortVo;
import com.indexingsystem.boss.Vo.ResourceCategorsVo;
import com.indexingsystem.boss.Vo.ResourceCategoryAdditionalVo;
import com.indexingsystem.boss.Vo.ResourceRelationInfoVo;
import com.indexingsystem.boss.Vo.TreeOrderQueryVo;
import com.indexingsystem.boss.Vo.TreeParentOrderQueryVo;
import com.indexingsystem.boss.Vo.TreeQueryVo;
import com.indexingsystem.boss.admin.mapper.ResCategoryRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourceCategorsMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.ResourceCategors;
import com.indexingsystem.boss.entity.ResourceCategorys;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.InstitutionLoadUtils;
import com.indexingsystem.boss.utils.JsonUtils;
import com.indexingsystem.boss.utils.StringUtil;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoPage;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.indexingsystem.db.mongoDB.impl.InstitutionsMongo;
import com.indexingsystem.db.mongoDB.impl.ResourceCategorsMongo;
import com.indexingsystem.db.mongoDB.impl.ResourceCategorsResListMongo;
import com.indexingsystem.db.mongoDB.po.SublevelResourceCategorsPo;
import com.indexingsystem.system.service.IPavilionColumnsService;
import com.indexingsystem.system.service.IResourceCategorsService;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Service
public class ResourceCategorsServiceImpl implements IResourceCategorsService {
	private static MongoManager			manager	= new MongoManager();

	@Resource
	private ResourceCategorsMapper		resourceCategorsMapper;

	@Resource
	private ResCategoryRelationsMapper	resCategoryRelationsMapper;

	@Resource
	private IPavilionColumnsService		iPavilionColumnsService;

	/**
	 * 根据资源分类id删除资源分类.
	 * 
	 * @param resTypeId
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(String resTypeId) {
		return resourceCategorsMapper.deleteByPrimaryKey( resTypeId );
	}

	/**
	 * 新增资源分类.
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public int insertSelective(ResourceCategors record) {

		int result = 0;

		result += resourceCategorsMapper.insertSelective( record );

		List<ResourceCategorsVo> resourceCategorsVoList = new ArrayList<ResourceCategorsVo>();
		ResourceCategorsVo resourceCategorsVo = new ResourceCategorsVo();
		resourceCategorsVo.setCategoryOrder( record.getCategoryOrder() );
		resourceCategorsVo.setDescription( record.getDescription() );
		resourceCategorsVo.setIsPublications( record.getIsPublications() );
		resourceCategorsVo.setResCategoryId( record.getResCategoryId() );
		resourceCategorsVo.setResName( record.getResName() );
		resourceCategorsVoList.add( resourceCategorsVo );
		ResourceCategorsMongo.addResourceCategors( resourceCategorsVoList, InstitutionLoadUtils.getString( "institutionId" ) );

		return result;

	}

	/**
	 * 通过资源分类id获取资源分类详情.
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public ResourceCategorsVo getResCategoryById(String resCategoryId) {
		return resourceCategorsMapper.getResCategoryById( resCategoryId );
	}

	/**
	 * 订单管理产品列表未购列表分页列表.
	 * 
	 * @param pageResult
	 * @param treeOrderQueryVo
	 * @return
	 */
	@Override
	public NotBugOrderProductListVo getOrderProductBugVoList(PagedResult<ResourceCategors> pageResult, TreeOrderQueryVo treeOrderQueryVo) {
		List<ResourceCategors> functionesList = null;
		List<OrderProductBugVo> orderProductBugVoList = null;
		List<OrderProductBugVo> resultOrderProductBugVoList = null;
		List<String> resCategoryIdList = null;
		List<String> resultResCategoryIdList = null;
		ResourceCategoryAdditionalVo resAppendVo = null;
		OrderProductBugVo orderProductBugVo = null;
		NotBugOrderProductListVo notBugOrderProudctListVo = new NotBugOrderProductListVo();

		// 如果为一级，默认以下处理
		if (null != pageResult) {
			functionesList = (List<ResourceCategors>) pageResult.getDataList();
			if (null != functionesList && functionesList.size() > 0) {
				LogCvt.info( "开始通过id查询子级订单未购资源分类列表: 3333333333333333333333=" + functionesList );
				orderProductBugVoList = new ArrayList<OrderProductBugVo>();
				for (ResourceCategors resCategors : functionesList) {
					resCategoryIdList = new ArrayList<String>();
					int exitesCount = resourceCategorsMapper.getResourceCategorsResRelationByResCategoryIdExitsResource( resCategors
							.getResCategoryId() );
					if (exitesCount > 0) {
						resultResCategoryIdList = resourceCategorsMapper.getOrderResourceCategorsIdListByCategoryOrder( resCategors
								.getCategoryOrder() );
						if (null != resultResCategoryIdList && resultResCategoryIdList.size() > 0) {
							for (String resCateId : resultResCategoryIdList) {
								if (!resCategoryIdList.contains( resCateId )) {
									resCategoryIdList.add( resCateId );
								}
							}

							if (null != resCategoryIdList && resCategoryIdList.size() > 0) {
								OrderProductListQueryVo orderProductListQueryVo = new OrderProductListQueryVo();
								if (StringUtils.isNotBlank( treeOrderQueryVo.getBeginTime() )) {
									orderProductListQueryVo.setBeginTime( treeOrderQueryVo.getBeginTime() );
								}
								if (StringUtils.isNotBlank( treeOrderQueryVo.getEndTime() )) {
									orderProductListQueryVo.setEndTime( treeOrderQueryVo.getEndTime() );
								}
								if (StringUtils.isNotBlank( treeOrderQueryVo.getQueryCriteria() )) {
									orderProductListQueryVo.setQueryCriteria( treeOrderQueryVo.getQueryCriteria() );
								}
								orderProductListQueryVo.setList( resCategoryIdList );
								// 通过资源分类类型分别处理：
								if (resCategors.getIsPublications() == 1) {
									resAppendVo = resourceCategorsMapper.getMagazinesResCategoryAddiionalByResCateId( orderProductListQueryVo );
								} else {
									// 获取开始年份、结束年份、价格总和
									resAppendVo = resourceCategorsMapper.getResCategoryAddiionalByResCateId( orderProductListQueryVo );
								}
							}
						}
						if (StringUtil.isNotBlank( resAppendVo )) {

							if (!resAppendVo.getBeginYear().equals( "0" )) {
								orderProductBugVo = new OrderProductBugVo();
								orderProductBugVo.setBeginYear( resAppendVo.getBeginYear() );
								orderProductBugVo.setEndYear( resAppendVo.getEndYear() );
								orderProductBugVo.setPrice( resAppendVo.getTotalPrice() );
								orderProductBugVo.setProductId( resCategors.getResCategoryId() );
								orderProductBugVo.setProductName( resCategors.getResName() );
								if (!orderProductBugVoList.contains( orderProductBugVo )) {
									orderProductBugVoList.add( orderProductBugVo );
								}
							}
						}
					}
				}
			}
		} else {
			LogCvt.info( "开始通过id查询子级订单未购资源分类列表: 44444444444444444444444444=" + treeOrderQueryVo.getId() );
			int exitesCount = resourceCategorsMapper.getResourceCategorsResRelationByResCategoryIdExitsResource( treeOrderQueryVo.getId() );
			if (exitesCount > 0) {
				resCategoryIdList = new ArrayList<String>();
				orderProductBugVoList = new ArrayList<OrderProductBugVo>();
				ResourceCategors resCategors = resourceCategorsMapper.selectByPrimaryKey( treeOrderQueryVo.getId() );
				if (null != resCategors) {
					resultResCategoryIdList = resourceCategorsMapper.getOrderResourceCategorsIdListByCategoryOrder( resCategors.getCategoryOrder() );
					if (null != resultResCategoryIdList && resultResCategoryIdList.size() > 0) {
						for (String resCateId : resultResCategoryIdList) {
							if (!resCategoryIdList.contains( resCateId )) {
								resCategoryIdList.add( resCateId );
							}
						}

						if (null != resCategoryIdList && resCategoryIdList.size() > 0) {
							OrderProductListQueryVo orderProductListQueryVo = new OrderProductListQueryVo();
							// 通过资源分类类型分别处理：
							if (resCategors.getIsPublications() == 1) {
								if (StringUtils.isNotBlank( treeOrderQueryVo.getBeginTime() )) {
									orderProductListQueryVo.setBeginTime( treeOrderQueryVo.getBeginTime() );
								}
								if (StringUtils.isNotBlank( treeOrderQueryVo.getEndTime() )) {
									orderProductListQueryVo.setEndTime( treeOrderQueryVo.getEndTime() );
								}
								if (StringUtils.isNotBlank( treeOrderQueryVo.getQueryCriteria() )) {
									orderProductListQueryVo.setQueryCriteria( treeOrderQueryVo.getQueryCriteria() );
								}
								orderProductListQueryVo.setList( resCategoryIdList );

								resAppendVo = resourceCategorsMapper.getMagazinesResCategoryAddiionalByResCateId( orderProductListQueryVo );
							} else {
								// 获取开始年份、结束年份、价格总和
								if (StringUtils.isNotBlank( treeOrderQueryVo.getBeginTime() )) {
									orderProductListQueryVo.setBeginTime( treeOrderQueryVo.getBeginTime() );
								}
								if (StringUtils.isNotBlank( treeOrderQueryVo.getEndTime() )) {
									orderProductListQueryVo.setEndTime( treeOrderQueryVo.getEndTime() );
								}
								if (StringUtils.isNotBlank( treeOrderQueryVo.getQueryCriteria() )) {
									orderProductListQueryVo.setQueryCriteria( treeOrderQueryVo.getQueryCriteria() );
								}
								orderProductListQueryVo.setList( resCategoryIdList );
								resAppendVo = resourceCategorsMapper.getResCategoryAddiionalByResCateId( orderProductListQueryVo );
							}
						}

						if (StringUtil.isNotBlank( resAppendVo )) {
							if (!resAppendVo.getBeginYear().equals( "0" )) {
								orderProductBugVo = new OrderProductBugVo();
								orderProductBugVo.setBeginYear( resAppendVo.getBeginYear() );
								orderProductBugVo.setEndYear( resAppendVo.getEndYear() );
								orderProductBugVo.setPrice( resAppendVo.getTotalPrice() );
								orderProductBugVo.setProductId( resCategors.getResCategoryId() );
								orderProductBugVo.setProductName( resCategors.getResName() );
								if (!orderProductBugVoList.contains( orderProductBugVo )) {
									orderProductBugVoList.add( orderProductBugVo );
								}
							}

						}
					}
				}
			}
		}
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( treeOrderQueryVo.getPageNumber(), treeOrderQueryVo.getPageSize() );
		Integer length = 0;
		Integer init = 0;
		if (null != orderProductBugVoList && orderProductBugVoList.size() > 0) {
			resultOrderProductBugVoList = new ArrayList<OrderProductBugVo>();
			if (orderProductBugVoList.size() >= treeOrderQueryVo.getPageSize()) {
				length = treeOrderQueryVo.getPageSize();
			} else {
				length = orderProductBugVoList.size();
			}

			if (treeOrderQueryVo.getPageNumber() - 1 > 0) {
				init = treeOrderQueryVo.getPageSize() * (treeOrderQueryVo.getPageNumber() - 1);
			} else {
				init = 0;
			}

			int k = 0;
			for (int j = init; k < length && j < orderProductBugVoList.size(); j++) {
				k++;
				resultOrderProductBugVoList.add( orderProductBugVoList.get( j ) );
			}

			notBugOrderProudctListVo.setMaxCount( orderProductBugVoList.size() );
			notBugOrderProudctListVo.setOrderProductBugVoPage( BeanUtils.orderToPagedResult( resultOrderProductBugVoList ) );
			return notBugOrderProudctListVo;
		} else {
			notBugOrderProudctListVo.setMaxCount( 0 );
			orderProductBugVoList = new ArrayList<OrderProductBugVo>();
			notBugOrderProudctListVo.setOrderProductBugVoPage( BeanUtils.toPagedResult( orderProductBugVoList ) );
			return notBugOrderProudctListVo;
		}
	}

	/**
	 * 通过资源分类id查询资源分类信息.
	 * 
	 * @param resTypeId
	 * @return
	 */
	@Override
	public ResourceCategors selectByPrimaryKey(String resCategoryId) {
		return resourceCategorsMapper.selectByPrimaryKey( resCategoryId );
	}

	/**
	 * 修改资源分类.
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(ResourceCategors record) {

		int result = 0;
		result += resourceCategorsMapper.updateByPrimaryKeySelective( record );
		ResourceCategorsMongo.updateResourceCategorsById( record, InstitutionLoadUtils.getString( "institutionId" ) );
		// 修改资源分类name mongo
		ResourceCategorsResListMongo.updateResCategorRelationsName( record, InstitutionLoadUtils.getString( "institutionId" ) );
		return result;

	}

	/**
	 * 获取父级最大排序值
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public String getParentCategorySortMax(String resCategoryId) {
		return resourceCategorsMapper.getParentCategorySortMax( resCategoryId );
	}

	/**
	 * 获取资源分类总数量.
	 * 
	 * @return
	 */
	@Override
	public Integer getResourceCategorsListCount() {
		List<ResourceCategors> resourceCategorsList = new ArrayList<ResourceCategors>();
		List<ResourceCategorsVo> resourceCategorsVoList = null;
		resourceCategorsVoList = ResourceCategorsMongo.findResourceCategorsAll( new HashMap<String, Object>(),
				InstitutionLoadUtils.getString( "institutionId" ) );
		if (resourceCategorsVoList == null || resourceCategorsVoList.isEmpty()) {
			return resourceCategorsVoList.size();
		}
		return resourceCategorsMapper.getResourceCategorsListCount();
	}

	/**
	 * 分页查询资源分类列表.
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public PagedResult<ResourceCategors> selectResourceCategorsList(Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( resourceCategorsMapper.selectResourceCategorsList() );
	}

	/**
	 * 查询子资源分类分页查询
	 * 
	 * @param resCategoryId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public PagedResult<ResourceCategors> selectSublevelResourceCategorsList(TreeQueryVo treeReqVo) {

		Integer pageNo = treeReqVo.getPageNumber() == null ? 1 : treeReqVo.getPageNumber();
		Integer pageSize = treeReqVo.getPageSize() == null ? 10 : treeReqVo.getPageSize();

		List<ResourceCategors> resourceCategorsList = new ArrayList<ResourceCategors>();
		List<ResourceCategorsVo> resourceCategorsVoList = null;
		resourceCategorsVoList = ResourceCategorsMongo.findResourceCategorsByPage( treeReqVo, InstitutionLoadUtils.getString( "institutionId" ) );
		if (resourceCategorsVoList == null || resourceCategorsVoList.isEmpty()) {
			resourceCategorsVoList = resourceCategorsMapper.getResourceCategorsList();
			if (null != resourceCategorsVoList && resourceCategorsVoList.size() > 0) {
				// 存储mogoDb
				ResourceCategorsMongo.addResourceCategors( resourceCategorsVoList, InstitutionLoadUtils.getString( "institutionId" ) );
			}
			PageHelper.startPage( pageNo, pageSize );
			resourceCategorsList = resourceCategorsMapper.getResourceCategorsByIdList( treeReqVo );
		} else {
			for (int i = 0; i < resourceCategorsVoList.size(); i++) {
				ResourceCategors categors = new ResourceCategors();
				categors.setCategoryOrder( resourceCategorsVoList.get( i ).getCategoryOrder() );
				categors.setDescription( resourceCategorsVoList.get( i ).getDescription() );
				categors.setIsPublications( resourceCategorsVoList.get( i ).getIsPublications() );
				categors.setParentId( resourceCategorsVoList.get( i ).getParentId() );
				categors.setResName( resourceCategorsVoList.get( i ).getResName() );
				categors.setResCategoryId( resourceCategorsVoList.get( i ).getResCategoryId() );
				resourceCategorsList.add( categors );
			}

		}
		return BeanUtils.toPagedResult( resourceCategorsList );
	}

	@Override
	public List<ResourceCategorsVo> getPublications(OrderCateReqVo orderCateReqVo) {
		return resourceCategorsMapper.getPublications( orderCateReqVo );
	}

	/**
	 * 获取资源分类上下级列表信息.
	 * 
	 * @return
	 */
	@Override
	public List<ResourceCategorsVo> getResourceCategorsList() {

		List<ResourceCategorsVo> resourceCategorsVoList = null;
		resourceCategorsVoList = ResourceCategorsMongo.findResourceCategorsAll( new HashMap<String, Object>(),
				InstitutionLoadUtils.getString( "institutionId" ) );
		if (resourceCategorsVoList == null || resourceCategorsVoList.isEmpty()) {
			resourceCategorsVoList = resourceCategorsMapper.getResourceCategorsList();
			if (null != resourceCategorsVoList && resourceCategorsVoList.size() > 0) {
				// 存储mogoDb
				ResourceCategorsMongo.addResourceCategors( resourceCategorsVoList, InstitutionLoadUtils.getString( "institutionId" ) );
			}
		}
		return resourceCategorsVoList;

	}

	/**
	 * 向上级机构倒推递归获取资源分类列表.
	 * 
	 * @param resCategorsList
	 * @param resCategoryId
	 * @return
	 */
	private List<ResourceCategorsVo> getParentResCategorsTreeListByResCategoryId(List<ResourceCategorsVo> resCategorsList, String resCategoryId) {
		ResourceCategorsVo resourceCategorsVo = null;
		while (!resCategoryId.equals( "0" )) {
			/*
			 * DBObject dbObject=new BasicDBObject(); dbObject.put( "id",
			 * "getParentResCategorsTreeListByResCategoryId" ); dbObject.put(
			 * "resCategoryId", resCategoryId ); DBObject
			 * dbObjectresult=manager.findOne( dbObject,
			 * MongoTableName.DH_RESOURCE_CATEGORYS_TREE );
			 */

			resourceCategorsVo = resourceCategorsMapper.getResCategoryById( resCategoryId );

			if (null != resourceCategorsVo) {
				if (!resCategorsList.contains( resourceCategorsVo )) {
					resCategorsList.add( resourceCategorsVo );
				}
				resCategoryId = resourceCategorsVo.getParentId();
			}
			getParentResCategorsTreeListByResCategoryId( resCategorsList, resCategoryId );
		}
		return resCategorsList;
	}

	/**
	 * 通过资源分类名称和英文名查询资源分类是否重复.
	 * 
	 * @param resName
	 * @return
	 */
	@Override
	public ResourceCategors selectResourceCategorsExits(String resName) {
		return resourceCategorsMapper.selectResourceCategorsExits( resName );
	}

	/**
	 * 通过资源分类排序查询资源分类排序是否重复.
	 * 
	 * @param categoryOrder
	 * @return
	 */
	@Override
	public ResourceCategors selectResourceCategorsOrderNumberExits(String categoryOrder) {
		return resourceCategorsMapper.selectResourceCategorsOrderNumberExits( categoryOrder );

	}

	/**
	 * 查询订单产品关系表中资源分类的所有子级分类.
	 * 
	 * @param categoryOrder
	 * @return
	 */
	@Override
	public List<ResourceCategors> getOrderResourceCategorsListByCategoryOrder(String categoryOrder) {
		return resourceCategorsMapper.getOrderResourceCategorsListByCategoryOrder( categoryOrder );
	}

	/**
	 * 查询订单产品关系表中资源分类的子级分类id.
	 * 
	 * @param categoryOrder
	 * @return
	 */
	@Override
	public List<String> getOrderResourceCategorsIdListByCategoryOrder(String categoryOrder) {
		return resourceCategorsMapper.getOrderResourceCategorsIdListByCategoryOrder( categoryOrder );
	}

	/**
	 * 通过资源idlist 获取所有子级分类的id集合.
	 * 
	 * @param resCategoryIdList
	 * @return
	 */
	@Override
	public List<ResourceCategors> getOrderResourceCategorsListByResCategoryIdList(List<String> resCategoryIdList) {
		return resourceCategorsMapper.getOrderResourceCategorsListByResCategoryIdList( resCategoryIdList );
	}

	/**
	 * 批量删除资源分类.
	 */
	@Override
	public int deleteResourceCategorsByIdBatch(List<String> resCategoryIdList) throws Exception {
		int result = 0;

		if (null != resCategoryIdList && resCategoryIdList.size() > 0) {
			for (String resCategoryId : resCategoryIdList) {
				result += resourceCategorsMapper.deleteByPrimaryKey( resCategoryId );
				resCategoryRelationsMapper.deleteResRelationByResCategoryId( resCategoryId );
			}
		}
		// 1.删除资源分类表
		// result =
		// resourceCategorsMapper.deleteResourceCategorsByIdBatch(resCategoryIdList);
		// 2.删除资源分类关系表
		// resCategoryRelationsMapper.deleteBatchResRelationByResCategoryId(resCategoryIdList);

		// 3、删除栏目资源分类关系表
		iPavilionColumnsService.deleteMenusRelationsByResCategoryIds( resCategoryIdList );

		// 先删除mongo数据
		ResourceCategorsMongo.deleteResourceCategorsByIds( resCategoryIdList, InstitutionLoadUtils.getString( "institutionId" ) );

		// 通过分类id删除资源分类关系表 Mongo
		ResourceCategorsResListMongo.deleteResCategoryRelationsByResCategoryId( resCategoryIdList, InstitutionLoadUtils.getString( "institutionId" ) );

		return result;
	}

	/**
	 * 获取子资源分类总数.
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public Integer getSublevelResourceCategorsListSize(TreeQueryVo treeReqVo) {
		DBObject OpeObj = new BasicDBObject();
		OpeObj.put( "id", "getSublevelResourceCategorsListSize" );
		OpeObj.put( "parentResourceCategorsId", treeReqVo.getId() );
		DBObject dBObjectResult = null;// manager.findOne(OpeObj,MongoTableName.DH_RESOURCE_CATEGORYS_TREE);
		if (null == dBObjectResult) {
			Integer count = resourceCategorsMapper.getSublevelResourceCategorsListSize( treeReqVo );
			OpeObj.put( "count", count );
			// manager.add( OpeObj, MongoTableName.DH_RESOURCE_CATEGORYS_TREE);
			return count;
		} else {
			return Integer.valueOf( dBObjectResult.get( "count" ).toString() );
		}

	}

	/**
	 * 获取资源分类中资源关系资源列表总数量.
	 * 
	 * @return
	 */
	@Override
	public Integer getResourceCategorsResRelationListCount(ResRelationQueryVo resRealtionQueryVo) {
		return resourceCategorsMapper.getResourceCategorsResRelationListCount( resRealtionQueryVo );
	}

	/**
	 * 获取子资源分类订单未购买总数.
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public Integer getNotBugSublevelResourceCategorsListSize(TreeOrderQueryVo treeOrderQueryVo) {
		return resourceCategorsMapper.getNotBugSublevelResourceCategorsListSize( treeOrderQueryVo );
	}

	/**
	 * 子级机构获取父级订单已购总数.
	 * 
	 * @param treeOrderQueryVo
	 * @return
	 */
	@Override
	public Integer getParentNotBugSublevelResourceCategorsListSize(TreeParentOrderQueryVo treeParentOrderQueryVo) {
		return resourceCategorsMapper.getParentNotBugSublevelResourceCategorsListSize( treeParentOrderQueryVo );
	}

	/**
	 * 子级机构获取父级订单已购列表.
	 * 
	 * @param treeParentOrderQueryVo
	 * @return
	 */
	@Override
	public PagedResult<ResourceCategors> getParentNotBugSublevelResourceCategorsList(TreeParentOrderQueryVo treeParentOrderQueryVo) {
		Integer pageNo = treeParentOrderQueryVo.getPageNumber() == null ? 1 : treeParentOrderQueryVo.getPageNumber();
		Integer pageSize = treeParentOrderQueryVo.getPageSize() == null ? 10 : treeParentOrderQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( resourceCategorsMapper.getParentNotBugSublevelResourceCategorsList( treeParentOrderQueryVo ) );
	}

	/**
	 * 通过订单id、选择的分类id查询订单未购列表.
	 * 
	 * @param treeOrderQueryVo
	 * @return
	 */
	@Override
	public PagedResult<ResourceCategors> selectNotBugSublevelResourceCategorsList(TreeOrderQueryVo treeOrderQueryVo) {
		Integer pageNo = treeOrderQueryVo.getPageNumber() == null ? 1 : treeOrderQueryVo.getPageNumber();
		Integer pageSize = treeOrderQueryVo.getPageSize() == null ? 10 : treeOrderQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( resourceCategorsMapper.selectNotBugSublevelResourceCategorsList( treeOrderQueryVo ) );
	}

	/**
	 * 通过资源分类id查询资源分类包含的资源明细列表.
	 * 
	 * @param resCategoryId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public PagedResult<ResourceRelationInfoVo> getResourceRelationInfoVoListByResCategoryId(ResRelationQueryVo resRealtionQueryVo) {
		Integer pageNo = resRealtionQueryVo.getPageNumber() == null ? 1 : resRealtionQueryVo.getPageNumber();
		Integer pageSize = resRealtionQueryVo.getPageSize() == null ? 10 : resRealtionQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( resourceCategorsMapper.getResourceRelationInfoVoListByResCategoryId( resRealtionQueryVo ) );
	}

	/**
	 * 通过资源分类id所有子分类列表
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public List<ResourceCategors> getResourceCategorsList(String resCategoryId) {
		return resourceCategorsMapper.getResourceCategorsLists( resCategoryId );
	}

	/**
	 * 通过资源分类id获取分类中所有资源的开始年份、结束年份、价格总合附加属性.
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public ResourceCategoryAdditionalVo getResCategoryAddiionalByResCateId(OrderProductListQueryVo orderProductListQueryVo) {
		return resourceCategorsMapper.getResCategoryAddiionalByResCateId( orderProductListQueryVo );
	}

	/**
	 * 资源分类排序获取自身或目录排序资源分类列表.
	 * 
	 * @param resCategoryOrderQueryVo
	 * @return
	 */
	@Override
	public List<ResourceCategors> getResourceCategorsByCategoryOrder(ResCategoryOrderQueryVo resCategoryOrderQueryVo) {
		return resourceCategorsMapper.getResourceCategorsByCategoryOrder( resCategoryOrderQueryVo );
	}

	/**
	 * 批量修改资源分类上移、下移操作.
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateBatchResCategors(List<ResourceCategors> list) throws Exception {
		return resourceCategorsMapper.updateBatchResCategors( list );
	}

	/**
	 * 连续出版物类型通过资源分类id获取分类中所有资源的开始年份、结束年份、价格总合附加属性.
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public ResourceCategoryAdditionalVo getMagazinesResCategoryAddiionalByResCateId(OrderProductListQueryVo orderProductListQueryVo) {
		return resourceCategorsMapper.getMagazinesResCategoryAddiionalByResCateId( orderProductListQueryVo );
	}

	/**
	 * 通过资源分类id列表获取加入订单初始化开始年份、结束年份连续出版物类型资源信息.
	 * 
	 * @param resCategoryIdList
	 * @return
	 */
	@Override
	public OrderAddProductInitVo getMagazinesResCategoryProductInitInfo(List<String> resCategoryIdList) {
		return resourceCategorsMapper.getMagazinesResCategoryProductInitInfo( resCategoryIdList );
	}

	/**
	 * 通过资源分类id列表获取加入订单初始化开始年份、预约结束年份非连续出版物类型资源信息.
	 * 
	 * @param resCategoryIdList
	 * @return
	 */
	@Override
	public OrderAddProductInitVo getResCategoryProductInitInfo(List<String> resCategoryIdList) {
		return resourceCategorsMapper.getResCategoryProductInitInfo( resCategoryIdList );
	}

	/**
	 * 产品管理：资源分类中获取所有子级分类含自己的所有资源并去重资源列表总数量.
	 * 
	 * @param resCategorsRelationQueryVo
	 * @return
	 */
	@Override
	public Integer getResourceCategorsResRelationByResCategoryIdListCount(ResCategorsRelationQueryVo resCategorsRelationQueryVo) {
		return resourceCategorsMapper.getResourceCategorsResRelationByResCategoryIdListCount( resCategorsRelationQueryVo );
	}

	/**
	 * 产品管理：资源分类中获取所有子级分类含自己的所有资源并去重资源列表.
	 * 
	 * @param resCategorsRelationQueryVo
	 * @return
	 */
	@Override
	public PagedResult<ResourceRelationInfoVo> getResourceRelationInfoVoListByResCategoryIdlist(ResCategorsRelationQueryVo resCategorsRelationQueryVo) {
		Integer pageNo = resCategorsRelationQueryVo.getPageNumber() == null ? 1 : resCategorsRelationQueryVo.getPageNumber();
		Integer pageSize = resCategorsRelationQueryVo.getPageSize() == null ? 10 : resCategorsRelationQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		return BeanUtils.toPagedResult( resourceCategorsMapper.getResourceRelationInfoVoListByResCategoryIdlist( resCategorsRelationQueryVo ) );
	}

	/**
	 * 门户站点获取资源分类中所有子级分类含自己的所有资源并去重资源列表.
	 * 
	 * @param resCategorsRelationQueryVo
	 * @return
	 */
	@Override
	public List<ResourceRelationInfoVo> getResourceRelaInfoVoListByResCategoryIdlist(ResCategorsRelationQueryVo resCategorsRelationQueryVo) {
		return resourceCategorsMapper.getResourceRelationInfoVoListByResCategoryIdlist( resCategorsRelationQueryVo );
	}

	/**
	 * 产品管理树形中通过联系出版物或非连续出版物过滤分类.
	 * 
	 * @param isPublications
	 * @return
	 */
	@Override
	public List<ResourceCategorsVo> getResourceCategorsTreeList(Integer isPublications) {
		return resourceCategorsMapper.getResourceCategorsTreeList( isPublications );
	}

	/**
	 * 通过资源分类id校验资源分类是否有资源.
	 * 
	 * @param resCategoryId
	 * @return
	 */
	@Override
	public Integer getResourceCategorsResRelationByResCategoryIdExitsResource(String resCategoryId) {
		return resourceCategorsMapper.getResourceCategorsResRelationByResCategoryIdExitsResource( resCategoryId );
	}

	/**
	 * 栏目管理：只显示有资源的资源分类.
	 * 
	 * @param list
	 * @return
	 */
	@Override
	public List<ResourceCategorsVo> getMenuResourceCategorsTreeList(List<String> resCategoryIdList) {
		return resourceCategorsMapper.getMenuResourceCategorsTreeList( resCategoryIdList );
	}

	/**
	 * 获取产品列表中资源分类
	 * 
	 * @param resCategoryIdList
	 * @return
	 */
	@Override
	public List<ResourceCategorsVo> getResourceCategorsTreeList(List<String> resCategoryIdList) {
		return resourceCategorsMapper.getOrdersResourceCategorsTreeList( resCategoryIdList );
	}

	@Override
	public List<ResourceCategors> getResourceCategorsListByCondition(ResourceCategorsSortVo resourceCategorsSortVo) {
		// TODO Auto-generated method stub
		return resourceCategorsMapper.getResourceCategorsListByCondition( resourceCategorsSortVo );
	}

	@Override
	public void updateResourceCategorsListByCondition(ResourceCategorsSortVo resourceCategorsSortVo) {
		// TODO Auto-generated method stub
		resourceCategorsMapper.updateResourceCategorsListByCondition( resourceCategorsSortVo );
		ResourceCategorsMongo.deleteResourceCategorsAll( InstitutionLoadUtils.getString( "institutionId" ) );
		List<ResourceCategorsVo> resourceCategorsVoList = resourceCategorsMapper.getResourceCategorsList();
		if (null != resourceCategorsVoList && resourceCategorsVoList.size() > 0) {
			// 存储mogoDb
			ResourceCategorsMongo.addResourceCategors( resourceCategorsVoList, InstitutionLoadUtils.getString( "institutionId" ) );
		}

	}

	@Override
	public List<ResourceCategors> getResourceCategorsByOrderList(String orderId) {
		return resourceCategorsMapper.getResourceCategorsByOrderList( orderId );
	}

	@Override
	public ResourceCategors selectResourceCategorsByNameModel(ResourceCategors resourceCategors) {
		return resourceCategorsMapper.selectResourceCategorsByNameModel( resourceCategors );
	}

	@Override
	public String getCategorySortMax(String resCategoryId) {
		String maxCategorySort = "";
		ResourceCategors resourceCategors = null;
		StringBuffer resSb = null;
		String categoryOrder = "";
		Integer convertSort = 0;
		String leftSort = "";
		String rightSort = "";
		if (StringUtils.isNotBlank( resCategoryId )) {
			// 1、如果资源分类最上级都为空，则初始化001
			maxCategorySort = getParentCategorySortMax( resCategoryId );
			if (maxCategorySort.equals( "0" )) {
				resourceCategors = this.resourceCategorsMapper.selectByPrimaryKey( resCategoryId );
				if (null != resourceCategors) {
					resSb = new StringBuffer();
					resSb.append( resourceCategors.getCategoryOrder() ).append( "001" );
					categoryOrder = resSb.toString();
				} else {
					categoryOrder = "001";
				}
			} else {
				resSb = new StringBuffer();
				leftSort = maxCategorySort.substring( 0, maxCategorySort.length() - 3 );
				rightSort = maxCategorySort.substring( maxCategorySort.length() - 3, maxCategorySort.length() );
				convertSort = Integer.parseInt( rightSort ) + 1;
				if (convertSort > 0 && convertSort < 10) {
					resSb.append( leftSort ).append( "00" ).append( convertSort );
				} else if (convertSort >= 10 && convertSort < 100) {
					resSb.append( leftSort ).append( "0" ).append( convertSort );
				} else {
					if (convertSort <= 999) {
						resSb.append( leftSort ).append( convertSort );
					}
				}
				categoryOrder = resSb.toString();
			}
		}
		return categoryOrder;
	}

}